package BaseDatos;

import BaseDatos.DAO.MySQLDAOFactory;
import BaseDatos.DAO.OcaDAO;
import Servidor.Modelo.Games.Game;
import Servidor.Modelo.Users.User;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Angel Lopez
 */
public class OcaMySQLDAO implements OcaDAO{
    private Connection con;
    private boolean error = false;
    @Override
    public int insert_user(User user) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean delete_user(int id_user) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int modificate_user(User user) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public User select_user(int id_user) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ArrayList<User> select_list_users() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int insert_game(int id_owner) {
        
        int codigo = -1;
        int lineas_insertadas = 0;
        
        try{
            con = MySQLDAOFactory.createConnection();
            if (con==null)
                return -1;
            
            PreparedStatement stmtCP = (PreparedStatement) con.prepareStatement(QueriesMySQL.INSERT_GAME);

            stmtCP.setInt(1, id_owner);

            lineas_insertadas = stmtCP.executeUpdate();
            
            // AQUI DEVOLVEMOS EL ULTIMO CODIGO INSERTADO, TENEMOS QUE HACER UNA NUEVA CONSULTA
            if(lineas_insertadas==0){
                error=true;
            }        
            else{
                Statement stmt = (Statement) con.createStatement();
                ResultSet rs = stmt.executeQuery(QueriesMySQL.SELECT_GAME_LAST_INSERT);
                rs.next();
                codigo = rs.getInt(1);
            }
            
        
            
        }
        catch (SQLException e){System.out.println(e); error=true;}
        finally{
            if(!cerrarConexion()||(error)){
                return -1;
            }
        }
        
        return codigo;
    }
    
    @Override
    public boolean delete_game(int id_game) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int update_game_date_finish() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int update_game_winner() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ArrayList<Game> select_list_games() {
        ArrayList<Game> list_games = new ArrayList<Game>();
        try{
            con = MySQLDAOFactory.createConnection();
            if (con==null)
                return null;
                
            Statement stmt = (Statement) con.createStatement();
            ResultSet rs = stmt.executeQuery(QueriesMySQL.SELECT_GAMES);
            while(rs.next()){
                Game game_aux = new Game();
                game_aux.setId_game(rs.getInt("ID_GAME"));
                game_aux.setId_owner(rs.getInt("ID_OWNER"));
                game_aux.setId_winner(rs.getInt("ID_WINNER"));
                game_aux.setDate_start(rs.getString("DATE_START"));
                game_aux.setDate_start(rs.getString("DATE_FINISH"));
                game_aux.setActive(rs.getInt("ACTIVE"));
                list_games.add(game_aux);
            }
            
        }
        catch (SQLException e){System.out.println(e); error=true;}
        finally{
            if(!cerrarConexion()||(error)){
                return null;
            }
        }
        
        return list_games;
    }

    @Override
    public int insert_player(int id_game, int id_player) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    
    
    
    public boolean cerrarConexion(){
        try {
            if (con==null)
                return true;
            con.close();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(OcaMySQLDAO.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }


    
}
